var table = null;
var form = null;
var layer = null;
$(function () {
    chapter.initLayui();
});

var chapter = {
    /**
     * 初始化layui
     */
    initLayui: () => {
        layui.use(['table', 'form', 'layer'], function () {
            table = layui.table;
            form = layui.form;
            layer = layui.layer;
            // 加载表格
            chapter.queryChapterData();
            chapter.toolbarListener();
            chapter.barListener();
        });
    },
    /**
     * 监听按钮点击
     */
    toolbarListener: () => {
        table.on('toolbar(chapterTableFilter)', function (obj) {
            let checkStatus = table.checkStatus(obj.config.id);
            //获取选中的数据
            let data = checkStatus.data;
            switch (obj.event) {
                // 新增
                case 'add':
                    layer.open({
                        id: 'add',
                        type: 1,
                        title: ['新增'],
                        skin: 'layui-layer-lan',
                        area: '500px',
                        offset: 'auto',
                        content: $('#chapterDialog'),
                        btn: ['提交', '取消'],
                        success: (layero, index) => {
                            // 让按钮居中显示
                            layero.find('.layui-layer-btn').css('text-align', 'center');
                        },
                        end: function () {
                            $("#chapterForm")[0].reset();
                            $('#chapterDialog').hide();
                        },
                        yes: function (index, layero) {
                            let data = form.val('chapterForm');
                            // 向后台发送数据
                            jsonPost(data, '/chapter/addchapter', res => {
                                // 判断提交成功还是失败
                                if (res && res > 0) {
                                    layer.msg('新增成功', {icon: 1, time: 1500}, () => {
                                        $("#chapterForm")[0].reset();
                                        form.render();
                                        // 关闭所有弹窗
                                        layer.close(index);
                                        // 刷新表格数据
                                        table.reload('chapterTableId');
                                    })
                                } else {
                                    layer.msg('新增失败', {icon: 5, time: 1500}, () => {
                                        table.reload('userList');
                                    })
                                }
                            }, error => {
                                console.error(error)
                                layer.msg('服务器内部异常', {icon: 5, time: 1500})
                            })
                        },
                        btn2: function (index, layero) {
                            layer.close(index)
                        }

                    })
                    break;
                // 默认
                default:
                    console.log('无对应按钮事件')
                    break;
            }
        })
    },
    /**
     * 行点击监听
     */
    barListener: () => {
        //todo 监听行单击事件
        table.on('tool(chapterTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'update') {
                layer.open({
                    id: 'update',
                    type: 1,
                    title: ['修改'],
                    skin: 'layui-layer-lan',
                    area: '500px',
                    offset: 'auto',
                    content: $('#chapterDialog'),
                    btn: ['提交', '取消'],
                    success: (layero, index) => {
                        // 让按钮居中显示
                        layero.find('.layui-layer-btn').css('text-align', 'center');
                        form.val("chapterForm", {
                            "chapterid": data.chapterid,
                            "novelname": data.novelname,
                            "chapterlist": data.chapterlist,
                            "content": data.content
                        });
                    },
                    end: function () {
                        $("#chapterForm")[0].reset();
                        $('#chapterDialog').hide();
                    },
                    yes: function (index, layero) {
                        let _req = form.val('chapterForm');
                        _req.id = data.id;
                        // 向后台发送数据
                        jsonPost(_req, '/chapter/gotoupdatechapter', res => {
                            // 判断提交成功还是失败
                            if (res && res > 0) {
                                layer.msg('修改成功', {icon: 1, time: 1500}, () => {
                                    $("#chapterForm")[0].reset();
                                    form.render();
                                    // 关闭所有弹窗
                                    layer.close(index);
                                    // 刷新表格数据
                                    table.reload('chapterTableId');
                                })
                            } else {
                                layer.msg('修改失败', {icon: 5, time: 1500});
                            }
                        }, error => {
                            console.error(error)
                            layer.msg('服务器内部异常', {icon: 5, time: 1500})
                        })
                    },
                    btn2: function (index, layero) {
                        layer.close(index)
                    }
                })
            } else if (obj.event === 'delete') {
                layer.confirm('是否删除？', {title: '提示'}, function (index) {
                    get({id: data.id}, '/chapter/deletechapter', res => {
                        if (res && res > 0) {
                            layer.msg("删除成功", {icon: 1, time: 1500}, () => {
                                // 重新刷新表格
                                table.reload('chapterTableId');
                            })
                        } else {
                            layer.msg("删除失败", {icon: 5, time: 1500})
                        }
                    }, error => {
                        console.error(error)
                        layer.msg('服务器内部异常', {icon: 5, time: 1500})
                    })
                });
            }
        });
    },
    /**
     * 查询用户列表
     */
    queryChapterData: () => {
        let _req = form.val('queryChapterForm');
        table.render({
            id: 'chapterTableId',
            elem: '#chapterTable'
            , url: '/chapter/queryByPage'
            , toolbar: '#toolbar'
            , page: true
            , where: _req
            , method: 'get'
            , defaultToolbar: []
            , cols: [[ //表头
                {field: 'id', title: 'ID', align: 'center'}
                , {field: 'novelname', title: '小说名称', align: 'center'}
                , {field: 'chapterid', title: '章节ID', align: 'center'}
                , {field: 'chapterlist', title: '章节列表', align: 'center'}
                , {field: 'content', title: '章节内容', align: 'center'}
                , {title: '操作', align: 'center', toolbar: '#barDemo'}
            ]]
        });
    }
}
